﻿Imports CrystalDecisions.CrystalReports.Engine


''' <summary>
''' 來電統計報表
''' </summary>
''' <remarks></remarks>
Public Class frm_CSReportNew

    Implements IDServer.IDServer.IDServer

#Region "Variabled"

#Region "System Variabled Define"


    Public ActionHelp As IDServer.IDServer.ActionBtn = IDServer.IDServer.ActionBtn.ActionHelp

    Private WithEvents MainForm As Frm_MainForm

    Public Property SelectObject() As Frm_MainForm
        Get
            Return MainForm
        End Get
        Set(ByVal value As Frm_MainForm)
            MainForm = value
        End Set
    End Property

    Public action_help As IDServer.IDServer.ActionBtn = IDServer.IDServer.ActionBtn.ActionHelp

#End Region

#Region "user variabled define"

    ''' <summary>
    ''' 窗體是否加載
    ''' </summary>
    ''' <remarks></remarks>
    Private fLoadBool As Boolean = False

#End Region

#End Region

#Region "Methods"

#Region "Init"

    Private Sub Initilize()

        Me.dtp_start.Text = ServerDate
        Me.dtp_end.Text = ServerDate
        Me.dtp_start.Focus()

        AddHandlers(New Control() {Me.Panel1})
        Dim imeMode As clsIme = New clsIme()
        imeMode.AddHandler_ImeMode(New Control() {Me.Panel1})
        ' FillComboxForEnum(Me.ComboBox1, GetType(ModEnum.ReceiveType))

        Dim tb As New DataTable
        Dim sql As String = "select FName,fCode from CS_CusterServiceTypes where fParentCode='0'"
        'sql = "select FName,fCode from CS_CusterServiceTypes a where fLevel in ('0','1')" + vbNewLine
        'sql += "and exists" + vbNewLine
        'sql += "(select * from CS_CusterServiceTypes b where" + vbNewLine
        'sql += "b.fCode like a.fCode+'%' and a.fCode<>b.fCode " + vbNewLine
        'sql += ")" + vbNewLine

        tb = Exec(sql, E_ExecType.DataTable)
        Me.ComboBox1.DataSource = tb
        Me.ComboBox1.DisplayMember = "FName"
        Me.ComboBox1.ValueMember = "fCode"
        Me.ComboBox1.SelectedIndex = 0
    End Sub

#End Region

#Region "AddHandler Events"

    ''' <summary>
    ''' KeyDown Event
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub TextBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        Try
            Select Case e.KeyCode
                Case Keys.Enter
                    If e.Shift = True Then
                        If TypeOf sender Is ComboBox Then
                            If CType(sender, ComboBox).DroppedDown = True Then
                                CType(sender, ComboBox).DroppedDown = False
                            End If
                        End If
                        Me.ProcessTabKey(False)
                        SendKeys.Send("{F4}")
                    Else
                        If TypeOf sender Is ComboBox Then
                            If CType(sender, ComboBox).DroppedDown = True Then
                                CType(sender, ComboBox).DroppedDown = False
                            End If
                        End If
                        If TypeOf sender Is TextBox AndAlso CType(sender, TextBox).Multiline = True Then
                        Else
                            Me.ProcessTabKey(True)
                            SendKeys.Send("{F4}")
                        End If
                    End If
            End Select
        Catch ex As Exception
            MsgBox("TextBox_KeyDown:" & ex.Message & ex.StackTrace, MsgBoxStyle.Critical And MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 添加自定義事件    ''' </summary>
    ''' <param name="Pans"></param>
    ''' <remarks></remarks>
    Private Sub AddHandlers(ByVal Pans() As Control)
        Try
            For Each Pan As Control In Pans
                For Each Ctrl As Control In Pan.Controls
                    If TypeOf Ctrl Is TextBox _
                       OrElse TypeOf Ctrl Is ComboBox _
                       OrElse TypeOf Ctrl Is DateTimePicker Then
                        AddHandler Ctrl.KeyDown, AddressOf TextBox_KeyDown
                    End If
                Next
            Next
        Catch ex As Exception
            MsgBox("AddHandlers:" & ex.Message & ex.StackTrace, MsgBoxStyle.Critical And MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

#End Region

#Region "ExecFind"

    Private Function ReturnWhereExp() As String

        Dim sql As String = ""

        sql &= "  where 1=1 "
        sql &= "   and  ReceiveCallDate>=''" & Me.dtp_start.Text.Trim & "''"
        sql &= "  and   ReceiveCallDate<=''" & Me.dtp_end.Text.Trim & "''"


        If Me.txt_fEmployeeId.Text.Trim <> "" Then
            sql &= " and ( ListenPsn=''" & Me.txt_fEmployeeId.Text.Trim & "''  or BoNum in (select distinct BoNum from dbo.CS_CallBackInformation where Isnull(CallBackPsn,'''')=''" & Me.txt_fEmployeeId.Text.Trim & "'' ))"

        End If

        sql = "'" & sql & "'"

        'sql &= " ,'" & Me.txt_fEmployeeId.Text.Trim & "'"
        'sql &= " ," & I & ""
        'sql &= " ,'" & Format(Me.dtp_start.Value, "yyyy/MM/dd") & "-" & Format(Me.dtp_end.Value, "yyyy/MM/dd") & "'"
       
        If Not Me.ComboBox1.SelectedValue Is Nothing Then
            sql &= ",'" & Me.ComboBox1.SelectedValue.ToString & "'"
        End If



        Return sql
    End Function

    Private Sub ExecFind(ByVal whereExp As String)
        Try
            Me.Cursor = Cursors.WaitCursor
            Dim fileName As String = ""
            Dim sql As String = ""

            sql = "  exec [usp_ReportCSCallNew]  " & whereExp

            fileName = GetReportPath("CS", "ReportCSCall1.rpt")
            Dim myRepDoc As New CrystalDecisions.CrystalReports.Engine.ReportDocument()

            myRepDoc.Load(fileName, CrystalDecisions.Shared.OpenReportMethod.OpenReportByDefault)
            SetCrystalReportLoginfo(myRepDoc)
            Dim dtab As New DataTable
            dtab = Exec(sql, E_ExecType.DataTable)
            myRepDoc.SetDataSource(dtab)
            Me.CrystalReportViewer1.ReportSource = myRepDoc

            Dim Text_fEmployee As CrystalDecisions.CrystalReports.Engine.TextObject = myRepDoc.ReportDefinition.ReportObjects("Text_fEmployee")
            Dim Text_fCount As CrystalDecisions.CrystalReports.Engine.TextObject = myRepDoc.ReportDefinition.ReportObjects("Text_fCount")
            Dim Text_fDate As CrystalDecisions.CrystalReports.Engine.TextObject = myRepDoc.ReportDefinition.ReportObjects("Text_fDate")

            'Dim Text1 As CrystalDecisions.CrystalReports.Engine.TextObject = myRepDoc.ReportDefinition.ReportObjects("Text1")
            ''If I = 3 Or I = 4 Then
            ''    Text1.Text = ""
            ''End If
            'Text1.Text = ""
            Text_fEmployee.Text = Me.txt_fEmployeeId.Text.Trim
            Text_fDate.Text = Format(Me.dtp_start.Value, "yyyy/MM/dd") & "-" & Format(Me.dtp_end.Value, "yyyy/MM/dd")
            If Me.txt_fEmployeeId.Text.Trim <> "" Then
                Text_fCount.Text = "1"
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub BindGrid(ByVal dtab As DataTable)
        'Me.DataGridView1.DataSource = dtab
    End Sub
#End Region

#End Region

#Region "Events"

#Region "Form Load"

    Private Sub frm_CSReport_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            Initilize()
        Catch ex As Exception
            MessageBox.Show(ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

#End Region

#Region "Botton"

    Private Sub btnFind_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFind.Click
        Dim whereExp As String = Me.ReturnWhereExp()
        ExecFind(whereExp)
    End Sub

#End Region

#End Region

#Region "Interface"

    Public Function ActionAdd() As Boolean Implements IDServer.IDServer.IDServer.ActionAdd
        If AddEnable = False Then
            MessageBox.Show("對不起，您沒有新增權限", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
            Exit Function
        End If

        SelectObject.ActionHelp = IDServer.IDServer.ActionBtn.ActionHelp
    End Function

    Public Function ActionCancel() As Boolean Implements IDServer.IDServer.IDServer.ActionCancel

    End Function

    Public Function ActionDelete() As Boolean Implements IDServer.IDServer.IDServer.ActionDelete

    End Function

    Public Function ActionEdit() As Boolean Implements IDServer.IDServer.IDServer.ActionEdit

    End Function

    Public Function ActionExport() As Boolean Implements IDServer.IDServer.IDServer.ActionExport

    End Function

    Public Function ActionFilter() As Boolean Implements IDServer.IDServer.IDServer.ActionFilter

    End Function

    Public Function ActionGoFirst() As Boolean Implements IDServer.IDServer.IDServer.ActionGoFirst

    End Function

    Public Function ActionGoLast() As Boolean Implements IDServer.IDServer.IDServer.ActionGoLast

    End Function

    Public Function ActionGoNext() As Boolean Implements IDServer.IDServer.IDServer.ActionGoNext

    End Function

    Public Function ActionGoPrevious() As Boolean Implements IDServer.IDServer.IDServer.ActionGoPrevious

    End Function

    Public Function ActionImport() As Boolean Implements IDServer.IDServer.IDServer.ActionImport

    End Function

    Public Function ActionInitialize() As Boolean Implements IDServer.IDServer.IDServer.ActionInitialize

    End Function

    Public Function ActionPrint() As Boolean Implements IDServer.IDServer.IDServer.ActionPrint

    End Function

    Public Function ActionSave() As Boolean Implements IDServer.IDServer.IDServer.ActionSave

    End Function

    Public Function ActionSelect() As Boolean Implements IDServer.IDServer.IDServer.ActionSelect

    End Function

#End Region

End Class
